package com.incar.chuxing.activity.personal;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.incar.chuxing.R;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlContainer;
import com.incar.chuxing.application.MyApplication;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.CarOrderDetailBean;
import com.incar.chuxing.utils.AppManager;
import com.incar.chuxing.utils.ImageLoader;
import com.incar.chuxing.utils.IntentKey;
import com.incar.chuxing.utils.LoadSirCallback.LoadingWithContentCallback;
import com.incar.chuxing.utils.LoadSirCallback.NetErrorCallback;
import com.incar.chuxing.utils.LoadSirCallback.ServerErrorCallback;
import com.incar.chuxing.utils.TimeUtils;
import com.incar.chuxing.utils.ToastUtils;
import com.kingja.loadsir.core.LoadSir;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TravelDetailActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.order_car_image)
    ImageView orderCarImage;
    @BindView(R.id.order_car_license)
    TextView orderCarLicense;
    @BindView(R.id.order_car_brand)
    TextView orderCarBrand;
    @BindView(R.id.order_num)
    TextView orderNum;
    @BindView(R.id.order_date)
    TextView orderDate;
    @BindView(R.id.order_money)
    TextView orderMoney;
    @BindView(R.id.order_distance_money)
    TextView orderDistanceMoney;
    @BindView(R.id.order_time_money)
    TextView orderTimeMoney;
    @BindView(R.id.order_bujimianpei_money)
    TextView orderBujimianpeiMoney;
    @BindView(R.id.order_out_range_money)
    TextView orderOutRangeMoney;
    @BindView(R.id.trigger_detail)
    RelativeLayout triggerDetail;
    @BindView(R.id.textView_detail)
    TextView textViewDetail;
    @BindView(R.id.trigger_detail_arrow)
    ImageView triggerDetailArrow;
    @BindView(R.id.order_coupon_money)
    TextView orderCouponMoney;
    @BindView(R.id.order_mile_bean)
    TextView orderMileBean;
    @BindView(R.id.mill_discount)
    RelativeLayout millDiscount;
    @BindView(R.id.order_pay_method)
    TextView orderPayMethod;
    @BindView(R.id.order_actual_money)
    TextView orderActualMoney;
    @BindView(R.id.fee_detail)
    LinearLayout feeDetail;
    @BindView(R.id.fee_zuidixiaofei)
    TextView feeZuidixiaofei;
    @BindView(R.id.order_detail_rank_iv)
    ImageView orderDetailRankIv;
    @BindView(R.id.scroll_content)
    NestedScrollView scrollContent;


    private String orderId;
    private Context mContext = this;

    @Override
    public int getLayoutId() {
        return R.layout.activity_car_order_detail;
    }

    @Override
    public void initView() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().finishActivity();
            }
        });
        loadService = LoadSir.getDefault().register(scrollContent, new com.kingja.loadsir.callback.Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                getCarOrderDetail();
            }
        });
        Intent intent = getIntent();
        orderId = intent.getStringExtra(IntentKey.INTENT_ORDER_ID);
        getCarOrderDetail();
    }

    private void getCarOrderDetail() {
        loadService.showCallback(LoadingWithContentCallback.class);
        ApiStore.getInstance().getApiService(UrlContainer.Request.BASE_GATE_URL).getTravelDetail(((MyApplication) getApplication()).getRequestFieldMap(), orderId, "1").enqueue(new Callback<CarOrderDetailBean>() {
            @Override
            public void onResponse(Call<CarOrderDetailBean> call, Response<CarOrderDetailBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        loadService.showSuccess();
                        CarOrderDetailBean item = response.body();
                        orderCarLicense.setText(item.getAttachment().getOrderFavourableModel().getCarModel().getLicenseTag());
                        orderCarBrand.setText(item.getAttachment().getOrderFavourableModel().getCarModel().getBrand() + item.getAttachment().getOrderFavourableModel().getCarModel().getModelCN());
                        ImageLoader.getInstance().displayImage(getApplicationContext(), ((MyApplication) getApplication()).getImageUrl() + item.getAttachment().getOrderFavourableModel().getCarModel().getPicture(), orderCarImage);
                        ImageLoader.getInstance().displayImage(getApplicationContext(), ((MyApplication) getApplication()).getImageUrl() + item.getAttachment().getOrderFavourableModel().getCarModel().getIcon(), orderDetailRankIv);
                        orderNum.setText(item.getAttachment().getOrderFavourableModel().getOrderNum());
                        orderDate.setText(TimeUtils.getFormatDate(item.getAttachment().getOrderFavourableModel().getOrderTime() + "", "yyyy年MM月dd日 HH:mm"));
                        //订单费用
                        orderMoney.setText("¥" + item.getAttachment().getOrderFavourableModel().getOrderPrice());
                        //时长费 里程费 还车附加费
                        orderDistanceMoney.setText("¥" + item.getAttachment().getOrderFavourableModel().getMileageMoney());
                        orderOutRangeMoney.setText("¥" + item.getAttachment().getOrderFavourableModel().getDispatchingFee());
                        orderTimeMoney.setText("¥" + item.getAttachment().getOrderFavourableModel().getDayMoney());
                        orderBujimianpeiMoney.setText("¥" + item.getAttachment().getOrderFavourableModel().getAbatement());
                        double licheng_fee = Double.parseDouble(item.getAttachment().getOrderFavourableModel().getMileageMoney());
                        double shichang_fee = Double.parseDouble(item.getAttachment().getOrderFavourableModel().getDayMoney());
                        double zuidi_fee = Double.parseDouble(item.getAttachment().getOrderFavourableModel().getStartingPrice());
                        if (licheng_fee + shichang_fee < zuidi_fee) {
                            feeZuidixiaofei.setVisibility(View.VISIBLE);
                        } else {
                            feeZuidixiaofei.setVisibility(View.INVISIBLE);
                        }
                        //优惠券抵扣
                        if (item.getAttachment().getOrderFavourableModel().getCouponUserModel().getDenomination() != 0) {
                            orderCouponMoney.setText("- ¥" + item.getAttachment().getOrderFavourableModel().getCouponUserModel().getDenomination() + "");
                        } else {
                            orderCouponMoney.setText("无");
                            orderCouponMoney.setTextColor(Color.parseColor("#999999"));
                        }


                        //里程豆抵扣
                        if (Double.parseDouble(item.getAttachment().getOrderFavourableModel().getMileageBeanPaiedMoney()) != 0) {
                            millDiscount.setVisibility(View.VISIBLE);
                            orderMileBean.setText("- ¥" + item.getAttachment().getOrderFavourableModel().getMileageBeanPaiedMoney() + "");
                        } else {
                            millDiscount.setVisibility(View.GONE);
                        }

                        //支付方式
                        if (item.getAttachment().getOrderFavourableModel().getPayType() == 1) {
                            orderPayMethod.setText("实付（支付宝）");
                        } else if (item.getAttachment().getOrderFavourableModel().getPayType() == 2) {
                            orderPayMethod.setText("实付（微信）");
                        } else if (item.getAttachment().getOrderFavourableModel().getPayType() == 3) {
                            orderPayMethod.setText("实付（余额）");
                        }

                        orderActualMoney.setText("¥" + item.getAttachment().getOrderFavourableModel().getRealPrice());
                    } else {
                        loadService.showCallback(ServerErrorCallback.class);
                        ToastUtils.makeText(mContext, response.body().getMessage(), ToastUtils.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<CarOrderDetailBean> call, Throwable t) {
                loadService.showCallback(NetErrorCallback.class);
            }
        });
    }

    @OnClick({R.id.trigger_detail})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.trigger_detail:
                if (feeDetail.isShown()) {
                    feeDetail.setVisibility(View.GONE);
                    triggerDetailArrow.setImageDrawable(getResources().getDrawable(R.drawable.pull_down));
                    textViewDetail.setText("详情");
                } else {
                    feeDetail.setVisibility(View.VISIBLE);
                    triggerDetailArrow.setImageDrawable(getResources().getDrawable(R.drawable.pull_up));
                    textViewDetail.setText("收起");
                }
                break;
        }
    }
}
